﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Configuration;

namespace WriteLog
{
    public class LogControl : ConfigurationSection
    {
        [ConfigurationProperty("WriteToType", IsRequired = true)]
        public string WriteToType
        {
            get 
            {
                return base["WriteToType"] as string;
            }
        }
        private WriteToType WriteTo
        {
            get 
            {
                string enumItem = this.WriteToType;
                return (WriteToType)Enum.Parse(typeof(WriteToType), enumItem, true);
            }
        }

        [ConfigurationProperty("TextFile", IsRequired = false)]
        private SaveLogToTextFile TextFile
        {
            get { return (SaveLogToTextFile)base["TextFile"]; }
        }

        public ISaveLog LogWriter
        {
            get
            {
                try
                {
                    switch (WriteTo)
                    {
                        case WriteLog.WriteToType.TextLogFile:
                            return TextFile;
                        case WriteLog.WriteToType.DataBase:
                        case WriteLog.WriteToType.Event:
                            throw new Exception("还没有实现。");
                        default:
                            return TextFile;
                    }
                }
                catch(Exception ex)
                {
                    Console.WriteLine("出现错误：{0}{1}日志记录逻辑将不做任何处理。",ex.Message,System.Environment.NewLine);
                    return new NullSaveLog();
                }
            }
        }
    }

    /// <summary>
    /// 日志存储的位置
    /// </summary>
    public enum WriteToType
    { 
        /// <summary>
        /// 写入到文本文件
        /// </summary>
        TextLogFile = 0,
        /// <summary>
        /// 写入到数据库
        /// </summary>
        DataBase = 1,
        /// <summary>
        /// 写入到windows事件
        /// </summary>
        Event = 2
    }
}
